#include "mainWindow.h"


/** constructeur, initialized/resized/paint sont des modifications du code du site suivant
 * http://fhalgand.developpez.com/tutoriels/qt/integration-opengl/01-premiere-application/
 */


mainWindow::mainWindow(char *fichier, QWidget *parent)
    : myGLWidget(60, parent, "Shepp Logan avec OpenGL et Qt"),_image( fichier )
{
}



void mainWindow::initializeGL()
{

    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    glClearDepth(1.0f);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );

}

void mainWindow::resizeGL(int width, int height)
{
    if(height == 0)
        height = 1;
    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(80.0f, (GLfloat)width/(GLfloat)height, 0.1f, 1000.0f);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

void mainWindow::paintGL()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    glClearDepth(1.0f);
    glLoadIdentity();
    gluLookAt(_cam.getPos(0),
              _cam.getPos(1),
              _cam.getPos(2),0,0,0,0,1,0);

    _cam.rotationX();
    _cam.rotationY();

    unsigned facesDrawn = _image.draw();


    glColor3f(1,1,1);
    QString info  = "Nombre de faces\t: " + QString::number(facesDrawn);
    QString info1 = "Coupe a la couche\t: " + QString::number(_image.getLayer());

    renderText(10,20,info);
    renderText(10,35,info1);

}


void mainWindow::timeOutSlot() {
    updateGL();
}



void mainWindow::keyPressEvent(QKeyEvent *keyEvent){
    switch(keyEvent->key())
    {
        case Qt::Key_Escape:
            close();
            break;
    case Qt::Key_Plus :
        _image.addLayer();
        break;
    case Qt::Key_Minus :
        _image.removeLayer();
    }
}



void mainWindow::mousePressEvent(QMouseEvent *e) {
    _cam.updatePosCam(e->pos());
}


void mainWindow::mouseMoveEvent(QMouseEvent *e) {
    _cam.rotate(e);

}


void mainWindow::wheelEvent(QWheelEvent *event) {
    int numDegrees = event->delta();

    if (numDegrees < 0)
        _cam.addDistance(3);

    else
        _cam.addDistance(-3);

}

